<?php

namespace StarCloud\Library\Sdk;

use AlibabaCloud\SDK\ICE\V20201109\ICE;
use AlibabaCloud\SDK\ICE\V20201109\Models\GetMediaProducingJobRequest;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\ICE\V20201109\Models\SubmitMediaProducingJobRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class AliMergeVideo
{
    /**
     * 使用AK&SK初始化账号Client
     * @return ICE Client
     */
    public static function createClient(){
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html。
        $config = new Config([
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
            "accessKeyId" => 'LTAI5tAVLJdnByK5g1VZbHuy',
            // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            "accessKeySecret" => 'iyJJcUTBi76tadziwuxBJhy5LvpTi1'
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/ICE
        $config->endpoint = "ice.cn-shanghai.aliyuncs.com";
        return new ICE($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function create($timeLine, $outputMediaConfig){
        $client = self::createClient();
        $submitMediaProducingJobRequest = new SubmitMediaProducingJobRequest([
//            "timeline" => "{\"VideoTracks\":[{\"VideoTrackClips\":[{\"MediaURL\":\"https://doumvip-video.oss-cn-shanghai.aliyuncs.com/fengshuang/novel_backgroud.jpeg\",\"Type\":\"Image\",\"Duration\":0.5,\"Effects\":[{\"Type\":\"Background\",\"SubType\":\"Blur\",\"Radius\":0.1},{\"Type\":\"Text\",\"Y\":0.5,\"Font\":\"WenQuanYi Zen Hei Mono\",\"Content\":\"书名#丰霜大帝\",\"FontSize\":120,\"BorderStyle\":3,\"FontColor\":\"#4B0082\",\"OutlineColour\":\"#FFDEAD\",\"Shadow\":3,\"BackColour\":\"#FFF0F5\",\"Alignment\":\"CenterCenter\",\"FontFace\":{\"Bold\":false,\"Italic\":false,\"Underline\":false}}]},{\"MediaURL\":\"https:\/\/doumvip-video.oss-cn-shanghai.aliyuncs.com\/fengshuang\/003.mp4\",\"In\":0.5,\"Out\":61,\"ClipId\":\"audio_1\",\"Effects\":[{\"Type\":\"Background\",\"SubType\":\"Blur\",\"Radius\":0.1},{\"Type\":\"Text\",\"Y\":0.25,\"Font\":\"WenQuanYi Zen Hei Mono\",\"Content\":\"书名#丰霜大帝\",\"FontSize\":120,\"BorderStyle\":3,\"FontColor\":\"#4B0082\",\"OutlineColour\":\"#FFDEAD\",\"Shadow\":3,\"BackColour\":\"#FFF0F5\",\"Alignment\":\"TopCenter\",\"FontFace\":{\"Bold\":false,\"Italic\":false,\"Underline\":false}},{\"Type\":\"Volume\",\"Gain\":0.2}]}]}],\"AudioTracks\":[{\"AudioTrackClips\":[{\"MediaUrl\":\"https://doumvip-video.oss-cn-shanghai.aliyuncs.com/fengshuang/demo2.mp3\",\"ReferenceClipId\":\"audio_1\",\"Effects\":[{\"Type\":\"Volume\",\"Gain\":3}]}]}],\"SubtitleTracks\":[{\"SubtitleTrackClips\":[{\"Type\":\"Subtitle\",\"SubType\":\"srt\",\"FileUrl\":\"https://doumvip-video.oss-cn-shanghai.aliyuncs.com/fengshuang/121533-ffmYR-0411.srt.srt\"}]}]}",
//            "outputMediaConfig" => "{\"MediaURL\":\"https://doumvip-video.oss-cn-shanghai.aliyuncs.com/fengshuang/demo32.mp4\"}"
            "timeline" => $timeLine,
            "outputMediaConfig" => $outputMediaConfig,
        ]);

        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $result = $client->submitMediaProducingJobWithOptions($submitMediaProducingJobRequest, $runtime);
            return $result->body->jobId;
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            var_dump($error->message);
            var_dump($error->getTraceAsString());
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }

        return false;
    }

    public static function queryJob($jobId)
    {
        $client = self::createClient();
        $getMediaProducingJobRequest = new GetMediaProducingJobRequest([
            "jobId" => $jobId
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $result = $client->getMediaProducingJobWithOptions($getMediaProducingJobRequest, $runtime);

            var_dump($result);
            return [$result->body->mediaProducingJob->status, $result->body->mediaProducingJob->mediaURL];
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            var_dump($error->message);
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }

        return [false, false];
    }
}

